##########################################################################################################################################
###### This is the file containing panel regression results for daily betas
###############################
# It creates the final results for Table 11
# Please note that in some instances, the paper contains some formatting modifications
# such as a "log()" that is not automatically added here or that we present the negative
# of tsim, while most output here is generated for the positive

# This is just the final step. There are other code files that are used for data preparation
# This processed data are saved and loaded in this code

#######
# choose the horizon. You have to run it for both to get the whole of Table 11
horizon="24Mon"
#horizon="60Mon"
######

# set to your working directory:
setwd("C:\\Users\\Admin\\OneDrive\\Desktop\\Market Power and Systematic Risk - Data and Code")
getwd()

load("Control_Panel_Daily.RData")

library(plm)
library(lfe)

########################### Univariate ###########################

Market_Power = data.frame(Beta_M = rep(NA,20), Beta_Up = rep(NA,20), Beta_Down = rep(NA,20), 
                          Weighting_Beta_M = rep(NA,20), Weighting_Beta_Up = rep(NA,20), Weighting_Beta_Down = rep(NA,20),
                          Shrinking_Beta_M = rep(NA,20), Shrinking_Beta_Up = rep(NA,20), Shrinking_Beta_Down = rep(NA,20),
                          Weighting_Shrinking_Beta_M = rep(NA,20), Weighting_Shrinking_Beta_Up = rep(NA,20), Weighting_Shrinking_Beta_Down = rep(NA,20))

rownames(Market_Power) = c("log(HHI)","","log(HHI)^2","1","R^2","prodmktfluid","2","prodmktfluid^2","3","2 R^2",
                           "tnic3hhi","4","tnic3hhi^2","5","3 R^2","tsim","6","tsim^2","7","4 R^2")

Temp = Panel_New[,c("Time","YearMonth","year","NAICS","PERMNO","Weighting_Beta_M_24M",
                    "HHI","prodmktfluid","tnic3hhi","tnic3tsimm",
                    "Age","Momentum","Illiquidity","MktCap","Mkt_Book","Default_Spread","Financial_Leverage", "Operating_Leverage","Firm_size","InvestmentRates","ROE","Logat","Dividend_Dummy","q","I_Vol")]#

if(horizon=="24Mon"){Temp$Weighting_Beta_M=Temp$Weighting_Beta_M_24M}
if(horizon=="60Mon"){Temp$Weighting_Beta_M=Temp$Weighting_Beta_M_60M}

for(i in 1:dim(Temp)[2]){
  Temp[,i][Temp[,i] == Inf] = NA
  Temp[,i][Temp[,i] == -Inf] = NA
}
Temp = na.omit(Temp)

Temp$HHI=scale(log(Temp$HHI))
a<-poly(Temp$HHI,2)
Temp$HHI2=a[,2]/sd(a[,2])
Temp$prodmktfluid=scale(Temp$prodmktfluid)
a<-poly(Temp$prodmktfluid,2)
Temp$prodmktfluid2=a[,2]/sd(a[,2])
Temp$tnic3hhi=scale(Temp$tnic3hhi)
a<-poly(Temp$tnic3hhi,2)
Temp$tnic3hhi2=a[,2]/sd(a[,2])
Temp$tnic3tsimm=scale(Temp$tnic3tsimm)
a<-poly(Temp$tnic3tsimm,2)
Temp$tnic3tsimm2=a[,2]/sd(a[,2])
Temp$Age=scale(log(Temp$Age))
Temp$Momentum=scale(Temp$Momentum)
Temp$Illiquidity=scale(Temp$Illiquidity)
Temp$MktCap=scale(log(Temp$MktCap))
Temp$Mkt_Book=scale(log(Temp$Mkt_Book))
Temp$Financial_Leverage=scale(Temp$Financial_Leverage)
Temp$Operating_Leverage=scale(Temp$Operating_Leverage)
Temp$Default_Spread=scale(Temp$Default_Spread)
Temp$Firm_size=scale(Temp$Firm_size)
Temp$InvestmentRates=scale(Temp$InvestmentRates)
Temp$ROE=scale(Temp$ROE)
Temp$Logat=scale(Temp$Logat)
Temp$q=scale(Temp$q)
Temp$I_Vol=scale(Temp$I_Vol)

source("Table_Functions.R")


A_Uni = felm(Weighting_Beta_M ~  tnic3tsimm + tnic3tsimm2| PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")
B_Uni = felm(Weighting_Beta_M ~  HHI + HHI2| PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

A_Complete = felm(Weighting_Beta_M ~  tnic3tsimm + tnic3tsimm2 + ((Age)) + (Logat) + (Default_Spread) + (Dividend_Dummy) + (Financial_Leverage) + 
                    ((MktCap)) + (Illiquidity) + (InvestmentRates) + (I_Vol) + ((Mkt_Book)) + (Momentum) +
                    (Operating_Leverage) + (q) + (ROE) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

B_Complete = felm(Weighting_Beta_M ~  HHI + HHI2 + ((Age)) + (Logat) + (Default_Spread) + (Dividend_Dummy) + (Financial_Leverage) + 
                    ((MktCap)) + (Illiquidity) + (InvestmentRates) + (I_Vol) + ((Mkt_Book)) + (Momentum) +
                    (Operating_Leverage) + (q) + (ROE) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

AB_Complete = felm(Weighting_Beta_M ~  tnic3tsimm + tnic3tsimm2 + HHI + HHI2 + ((Age)) + (Logat) + (Default_Spread) + (Dividend_Dummy) + (Financial_Leverage) + 
                     ((MktCap)) + (Illiquidity) + (InvestmentRates) + (I_Vol) + ((Mkt_Book)) + (Momentum) +
                     (Operating_Leverage) + (q) + (ROE) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="cgm")


#Bottom: R^2 and below t-stat:
Market_Paper = data.frame(Coeff_Names = rep(NA,2*length(coef(AB_Complete))+2),ProdFluid = rep(NA,2*length(coef(AB_Complete))+2), 
                          HHI = rep(NA,2*length(coef(AB_Complete))+2), ProdFluidCom = rep(NA,2*length(coef(AB_Complete))+2), 
                          HHICom = rep(NA,2*length(coef(AB_Complete))+2), MultiProdHHI = rep(NA,2*length(coef(AB_Complete))+2))

Names = c("tsim","tsim^2","log(HHI)","log(HHI)^2","log(Age)","log(AT)","Default~spread","Dividend","Financial~leverage","log(Firm~size)","Illiquidity","Investment~rate","iVol",
          "log(Mkt/Book)","Momentum","Operating~leverage","q","ROE")

Market_Paper$Coeff_Names[seq(from=1,to=(nrow(Market_Paper)-2),by=2)] = Names
Market_Paper$Coeff_Names[(nrow(Market_Paper)-1)] = "R_sq"
Market_Paper$Coeff_Names[(nrow(Market_Paper))] = "Fixed-Effects"

Market_Paper$ProdFluid[c(1,3)] = paste0(as.character(Lag_zero(round(summary(A_Uni)$coefficients[,1],digits=3),digits=3)),star_function(summary(A_Uni)$coefficients[,4]))
Market_Paper$ProdFluid[c(2,4)] = paste0("(",Lag_zero(round(summary(A_Uni)$coefficients[,3],digits=3),digits=3),")")
Market_Paper$ProdFluid[nrow(Market_Paper)-1] = as.character(Lag_zero(round(summary(A_Uni)$r2adj,digits=4) * 100,digits=2))
Market_Paper$ProdFluid[nrow(Market_Paper)] = "Yes"

Market_Paper$HHI[c(5,7)] = paste0(as.character(Lag_zero(round(summary(B_Uni)$coefficients[,1],digits=3),digits=3)),star_function(summary(B_Uni)$coefficients[,4]))
Market_Paper$HHI[c(6,8)] = paste0("(",Lag_zero(round(summary(B_Uni)$coefficients[,3],digits=3),digits=3),")")
Market_Paper$HHI[nrow(Market_Paper)-1] = as.character(Lag_zero(round(summary(B_Uni)$r2adj,digits=4) * 100,digits=2))
Market_Paper$HHI[nrow(Market_Paper)] = "Yes"

Market_Paper$ProdFluidCom[c(1,3,seq(from=9,to=(nrow(Market_Paper)-2),by=2))] = paste0(as.character(Lag_zero(round(summary(A_Complete)$coefficients[,1],digits=3),digits=3)),star_function(summary(A_Complete)$coefficients[,4]))
Market_Paper$ProdFluidCom[c(2,4,seq(from=10,to=(nrow(Market_Paper)-2),by=2))] = paste0("(",Lag_zero(round(summary(A_Complete)$coefficients[,3],digits=3),digits=3),")")
Market_Paper$ProdFluidCom[nrow(Market_Paper)-1] = as.character(Lag_zero(round(summary(A_Complete)$r2adj,digits=4) * 100,digits=2))
Market_Paper$ProdFluidCom[nrow(Market_Paper)] = "Yes"

Market_Paper$HHICom[seq(from=5,to=(nrow(Market_Paper)-2),by=2)] = paste0(as.character(Lag_zero(round(summary(B_Complete)$coefficients[,1],digits=3),digits=3)),star_function(summary(B_Complete)$coefficients[,4]))
Market_Paper$HHICom[seq(from=6,to=(nrow(Market_Paper)-2),by=2)] = paste0("(",Lag_zero(round(summary(B_Complete)$coefficients[,3],digits=3),digits=3),")")
Market_Paper$HHICom[nrow(Market_Paper)-1] = as.character(Lag_zero(round(summary(B_Complete)$r2adj,digits=4) * 100,digits=2))
Market_Paper$HHICom[nrow(Market_Paper)] = "Yes"

Market_Paper$MultiProdHHI[seq(from=1,to=(nrow(Market_Paper)-2),by=2)] = paste0(as.character(Lag_zero(round(summary(AB_Complete)$coefficients[,1],digits=3),digits=3)),star_function(summary(AB_Complete)$coefficients[,4]))
Market_Paper$MultiProdHHI[seq(from=2,to=(nrow(Market_Paper)-2),by=2)] = paste0("(",Lag_zero(round(summary(AB_Complete)$coefficients[,3],digits=3),digits=3),")")
Market_Paper$MultiProdHHI[nrow(Market_Paper)-1] = as.character(Lag_zero(round(summary(AB_Complete)$r2adj,digits=4) * 100,digits=2))
Market_Paper$MultiProdHHI[nrow(Market_Paper)] = "Yes"

library(xtable)
print(xtable(Market_Paper),include.rownames=F,sanitize.text.function=function(x){x})

